package com.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.BuyCartBean;
import com.project.bean.StockShopBean;
import com.project.bean.UserBean;
import com.project.service.IBuyCartService;
import com.project.service.IStockShopService;
import com.project.service.ISupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("buyCart")
@Api(tags = "采购车")
public class BuyCartController {
    @Autowired
    private IBuyCartService buyCartService;
    @Autowired
    private IStockShopService shopService;

    @ApiOperation(value = "添加采购商品")
    @PostMapping("add")
    public String add(String operType , StockShopBean stockShopBean, MultipartFile face, Integer num, HttpServletRequest request) throws IOException {
        if(operType.equals("add")){
            //得到上传文件名
            String fileName = face.getOriginalFilename();
            //以时间毫秒数做文件名，后缀名不变，重命名上传文件，以防止文件重名覆盖
            fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
            //将上传文件的二进制数据，保存到指定文件中
            face.transferTo(new File("E:/mavenstudy/WebInvoicingSystem/src/img/"+fileName));
            stockShopBean.setImg(fileName);
            shopService.add(stockShopBean);
        }
        //从session中取出登录用户
        UserBean user = (UserBean) request.getSession().getAttribute("loginUser");
        if(operType.equals("update")){
            buyCartService.add(stockShopBean, user.getId(), num);
        }
        return "ok";
    }
    @ApiOperation(value = "按采购车ID删除采购车商品")
    @DeleteMapping("del")
    public String del(Integer id) {
        buyCartService.del(id);
        return "ok";
    }

    @ApiOperation(value = "按登录用户查看购物车所有记录")
    @GetMapping ("findById")
    public List<BuyCartBean> findById(Integer userId) {
        return buyCartService.findById(userId);
    }

    @ApiOperation(value = "修改采购车信息")
    @PutMapping ("update")
    public String update(Integer id, Integer price, Integer num) {
        buyCartService.update(id, price, num);
        return "ok";
    }


}
